package com.jbit.dm_160.controller;

import com.jbit.dm_160.model.dto.ItemDTO;
import com.jbit.dm_160.model.dto.OrderInfoDTO;
import com.jbit.dm_160.model.entity.Item;
import com.jbit.dm_160.model.entity.Order;
import com.jbit.dm_160.model.entity.User;
import com.jbit.dm_160.model.vo.OrderVO;
import com.jbit.dm_160.service.ItemService;
import com.jbit.dm_160.service.OrderService;
import com.jbit.dm_160.service.UserService;
import com.jbit.dm_160.utils.DateUtils;
import com.jbit.dm_160.utils.ResponseEntity;
import com.jbit.dm_160.utils.ResponseUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/order/api")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private UserService userService;
    @PostMapping("/v/createOrder")
    public ResponseEntity createOrder(@RequestBody OrderVO orderVO, @RequestHeader String token){
        String orderNo = orderService.addOrder(orderVO,token);
        Order order = new Order();
        order.setOrderNo(orderNo);
        return ResponseUtils.sendSuccess(order);
        //我从controller中拿到的是哪个实体类?=>OrderVO
        //但是我数据库添加需要哪个实体类?=>Order
        //需要做转换
    }
    @PostMapping("/v/queryOrderByOrderNo")
    public ResponseEntity<OrderInfoDTO> queryOrderByOrderNo(@RequestBody Order order){
        OrderInfoDTO orderInfoDTO = orderService.getOrderInfoByOrderNo(order.getOrderNo());
        return ResponseUtils.sendSuccess(orderInfoDTO);
    }
    @PostMapping("/v/queryOrderList")
    public ResponseEntity<List<OrderInfoDTO>> queryOrderList(@RequestBody OrderVO orderVO,@RequestHeader String token){
        User user = userService.getUserByToken(token);
        orderVO.setUserId(user.getId());
        List<OrderInfoDTO> list = orderService.getOrderInfos(orderVO);
        return ResponseUtils.sendSuccess(list);
    }


}
